import 'community_service.dart';
import 'technique_service.dart';

/// 评论服务中间件
/// 统一处理钓技学堂和钓友圈的评论功能
class CommentService {
  final CommunityService _communityService = CommunityService();
  final TechniqueService _techniqueService = TechniqueService();

  /// 获取评论列表
  /// [postId] 帖子ID
  /// [postType] 帖子类型：'technique' 或 'community'
  /// [pageNum] 页码
  /// [pageSize] 每页数量
  Future<Map<String, dynamic>> getComments({
    required int postId,
    required String postType,
    int pageNum = 1,
    int pageSize = 10,
  }) async {
    try {
      if (postType == 'technique') {
        // 钓技学堂评论
        final result = await _techniqueService.getComments(postId.toString());
        return {
          'success': true,
          'data': result['comments'] ?? [],
          'total': result['total'] ?? 0,
          'message': '获取成功',
        };
      } else {
        // 钓友圈评论
        final result = await _communityService.getPostComments(postId: postId);
        return {
          'success': result['success'] ?? false,
          'data': result['data'] ?? [],
          'total': result['total'] ?? 0,
          'message': result['message'] ?? '获取成功',
        };
      }
    } catch (e) {
      return {
        'success': false,
        'data': [],
        'total': 0,
        'message': '获取评论失败: $e',
      };
    }
  }

  /// 添加评论
  /// [postId] 帖子ID
  /// [postType] 帖子类型：'technique' 或 'community'
  /// [content] 评论内容
  /// [parentId] 父评论ID（可选）
  Future<Map<String, dynamic>> addComment({
    required int postId,
    required String postType,
    required String content,
    int? parentId,
  }) async {
    try {
      if (postType == 'technique') {
        // 钓技学堂评论
        return await _techniqueService.addComment(
          postId.toString(),
          content,
          parentId: parentId?.toString(),
        );
      } else {
        // 钓友圈评论
        return await _communityService.addComment(
          postId: postId,
          content: content,
          parentId: parentId,
        );
      }
    } catch (e) {
      return {
        'success': false,
        'message': '发表评论失败: $e',
        'data': null,
      };
    }
  }

  /// 点赞评论
  /// [commentId] 评论ID
  /// [postType] 帖子类型：'technique' 或 'community'
  Future<Map<String, dynamic>> toggleCommentLike({
    required int commentId,
    required String postType,
  }) async {
    try {
      if (postType == 'technique') {
        // 钓技学堂评论点赞
        final result = await _techniqueService.toggleCommentLike(commentId.toString());
        return {
          'success': true,
          'message': '操作成功',
          'data': result,
        };
      } else {
        // 钓友圈评论点赞
        return await _communityService.toggleLikeComment(commentId);
      }
    } catch (e) {
      return {
        'success': false,
        'message': '点赞失败: $e',
        'data': null,
      };
    }
  }

  /// 点赞帖子
  /// [postId] 帖子ID
  /// [postType] 帖子类型：'technique' 或 'community'
  Future<Map<String, dynamic>> togglePostLike({
    required int postId,
    required String postType,
  }) async {
    try {
      if (postType == 'technique') {
        // 钓技学堂点赞
        final result = await _techniqueService.toggleLike(postId.toString());
        return {
          'success': true,
          'message': '操作成功',
          'data': result,
        };
      } else {
        // 钓友圈点赞
        return await _communityService.toggleLikePost(postId);
      }
    } catch (e) {
      return {
        'success': false,
        'message': '点赞失败: $e',
        'data': null,
      };
    }
  }

  /// 收藏帖子
  /// [postId] 帖子ID
  /// [postType] 帖子类型：'technique' 或 'community'
  Future<Map<String, dynamic>> togglePostFavorite({
    required int postId,
    required String postType,
  }) async {
    try {
      if (postType == 'technique') {
        // 钓技学堂收藏
        final result = await _techniqueService.toggleFavorite(postId.toString());
        return {
          'success': true,
          'message': '操作成功',
          'data': result,
        };
      } else {
        // 钓友圈收藏
        return await _communityService.toggleFavoritePost(postId);
      }
    } catch (e) {
      return {
        'success': false,
        'message': '收藏失败: $e',
        'data': null,
      };
    }
  }
}